Knowledge Base

Working With Custom Hooks: OnPublishComplete

Unlike other before and after hooks, this custom hook is not triggered by an action, but rather waits for the action to be completed before it is triggered. Learn how to employ this hook to augment or enhance the post-publish process.


Publishing in the CMS is the final step to sending content out to an external environment, usually to an Ingeniux DSS.  

The publish can be run as incremental or full. Incremental publishes add new content that has been checked in and marked for publish to the specified publishing target since the last publish was run, while a full publish republishes every content item that is checked in and marked for publish to the specified publishing target. 

Administrators can control who is allowed to publish using permissions for incremental and full publishing, and who can publish to a specific target using security. This hook is for controlling anything outside of those controls. 

A publish is complete when all items have been published to the publishing folder, including all settings. 

How To Trigger the Hook  

This method is triggered through the following actions: 

  • Clicking Publish from the toolbar on a content item and completing the dialog. 
  • Right-clicking on a content item in the Site Tree or Asset Tree, selecting Publish and completing the dialog. 
  • Selecting a publishing target from Administration > Publishing System, pushing the New Publish button, and completing the dialog. 
  • Calling the publish() method from the CSAPI. 

When to Use This Custom Hook  

Use this hook to trigger actions that augment or enhance the final stages of a publish or post-publish processes. For example, this might include: 

  • Syncing information between systems after a publish has completed on one system  
  • Processing data relevant to a recent or ongoing publish  
  • Sending communications or notifications to relevant parties after a publish is completed  

Considerations 

Unlike other before and after hooks, this hook is not actually triggered by the action, but waits for the completion of the process. Keep this in mind as the code may not execute for some time after the action that triggered the publish itself. 

Be careful about putting too much code into this hook, as publishing is something you do not want to slow down in your system. 

Example  

Send an email to the user who triggered the publish after the publish is complete 

Users often want to know when publishes finish without having to watch the publish-in-progress in the publishing monitor.  

This hook will enable the user who triggered the publish to go about their work and be notified when the publish is completed via email. 

Use the current user session to get the contact information about the user and standard smtp emailer to send out the email notification. Put the attempt into a try/catch and gather error information if the email fails to send for any reason. 

string currentUserEmail = session.Site.CurrentUser.EmailAddress; 

  

try 

{ 

var formsSmtpServer = "mx.ingeniuxondemand.com"; 

var formsSmtpServerPort = 25; 

var formsSmtpEnableSSL = false; 

var formsSmtpUsername = ""; 

var formsSmtpPassword = ""; 

var fromEmail = "noreply@ingeniux.com"; 

  

SmtpClient smtpServer = new SmtpClient(formsSmtpServer); 

  

if (formsSmtpEnableSSL) 

{ 

smtpServer.Credentials = new System.Net.NetworkCredential( 

formsSmtpUsername, 

formsSmtpPassword); 

smtpServer.EnableSsl = true; 

} 

  

smtpServer.Port = formsSmtpServerPort; 

  

MailMessage mail = new MailMessage(); 

mail.From = new MailAddress(fromEmail); 

mail.To.Add(currentUserEmail); 

 

mail.Subject = "OnPublishComplete"; 

mail.Body = "<h2>Hello Your Publish has been completed</h2>"; 

mail.BodyEncoding = System.Text.Encoding.UTF8; 

mail.IsBodyHtml = true; 

  

smtpServer.Send(mail); 

  

} 

catch (Exception e) 

{ 

session.Info("Error sending email" + e); 

 } 

Ways to Extend the Functionality Further  

The messaging in this email is pretty simple. Consider capturing more information about the publish and adding it to the email communication, for example: 

  • Publishing target 
  • Date, time, and/or location  
  • A list of content items that published 
  • Errors that occurred because of the publish  
  • Items that were unable to publish (along with information about the issue) 

These are all great things to include in a publish notification – but there are plenty more. Tell us how you’re using this hook in the comments!  

  • PRODUCT: CMS
  • VERSION: CMS 10
  • RELEASE: 10.x
  • Published: September 13, 2023
  • LAST UPDATED: September 12, 2023
  • Comments: 0

Please login to comment

Comments


There are no comments yet.